package com.shicijiaoliu.controller;


import com.shicijiaoliu.pojo.Active;
import com.shicijiaoliu.pojo.result;
import com.shicijiaoliu.service.Activeservice;
import com.shicijiaoliu.utils.ThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/active")
@Validated
public class activecontroller {

    @Autowired
    private Activeservice activeservice;

    @GetMapping("/list")
    public result<Object> list(int countpage, int pagenumber,
                               @RequestParam(required = false) String name,
                               @RequestParam(required = false) String starttime,
                               @RequestParam(required = false) String endtime){
        List<Active> activelist = activeservice.findBypage(countpage,pagenumber,name,starttime,endtime);
        if(activelist == null){
            return result.error("获取页大于总页数");
        }else{
            int count = activeservice.getcount(name,starttime,endtime);
            Map<String,Object> data = new HashMap<>();
            data.put("total",count);
            data.put("items",activelist);
            return result.success(data);}
    }

    @PostMapping("/add")
    public result add(String name,String description,String starttime,String endtime,String bi){
        LocalDateTime stime = null;
        LocalDateTime etime = null;
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        stime = LocalDateTime.parse(starttime, fmt);
        etime = LocalDateTime.parse(endtime, fmt);
        Active active = new Active(name,description,stime,etime,bi);
        Map<String,Object> userdate = ThreadLocalUtils.getThreadLocal();
        active.setAId( Integer.parseInt(userdate.get("id").toString()));
        if(activeservice.findByname(name) == null ){
            activeservice.add(active);
            return result.success();
        }else {
            return result.error("活动已存在");
        }
    }

    @DeleteMapping("/del/{id}")
    public result del(@PathVariable("id")long id){
        if (activeservice.findByid(id) == null) {
            return result.error("没有找到该活动");
        } else {
            activeservice.delByid(id);
            return result.success();
        }
    }

    @PutMapping("/update")
    public result update(String name,long id,String description,String starttime,String endtime,String bi) {
        LocalDateTime stime = null;
        LocalDateTime etime = null;
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        stime = LocalDateTime.parse(starttime, fmt);
        etime = LocalDateTime.parse(endtime, fmt);
        Active active = new Active(name, id,description,stime,etime,bi);
        if (activeservice.findByid(id) == null) {
            return result.error("没有找到该活动");
        } else {
            activeservice.update(active);
            return result.success();
        }
    }

    @GetMapping("/lista")
    public result<Object> lista(){
        List<Active> actives = activeservice.getlist();
        return result.success(actives);
    }
}
